package controller;

import com.alibaba.fastjson.JSON;
import dao.ArticleDao;
import dao.UserDao;
import entity.Article;


import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet({"/main",""})
public class MainServlet extends HttpServlet {

    UserDao userDao = new UserDao();
    ArticleDao articleDao = new ArticleDao();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //调用articleDao中的方法获取文章列表
        ArrayList<Article> articles = articleDao.getArticles();
        request.setAttribute("articles",articles);

        //将dao层获取的文章数组添加request作用域
        int userTotal = UserDao.getUserTotal();
        request.setAttribute("userTotal",userTotal);



        /* 允许跨域的主机地址 */
        response.setHeader("Access-Control-Allow-Origin", "*");
        /* 允许跨域的请求⽅法GET, POST, HEAD 等 */
        response.setHeader("Access-Control-Allow-Methods", "*");
        /* 重新预检验跨域的缓存时间 (s) */
        response.setHeader("Access-Control-Max-Age", "3600");
        /* 允许跨域的请求头 */
        response.setHeader("Access-Control-Allow-Headers", "*");
        /* 是否携带cookie */
        response.setHeader("Access-Control-Allow-Credentials", "true");


        //请求跳转之前先调用dao层方法获取页面所需数据

        request.getRequestDispatcher("/main.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

}

